﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace SimpleMapper
{
    /// <summary>
    /// 映射程序扩展方法
    /// </summary>
    public static class MapperExtensions
    {
        public static Mapper _mapper = new Mapper();
        /// <summary>
        /// 将<see cref="DataTable"/>映射为<typeparamref name="TDestination"/>列表
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="table"></param>
        /// <returns></returns>
        public static List<TDestination> To<TDestination>(this DataTable table)
            where TDestination : class
        {
            return _mapper.To<TDestination>(table, false);
        }
        /// <summary>
        /// 将<see cref="DataRow"/>映射为<typeparamref name="TDestination"/>对象
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="row"></param>
        /// <returns></returns>
        public static TDestination To<TDestination>(this DataRow row)
            where TDestination : class
        {
            return _mapper.To<TDestination>(row, false);
        }
        /// <summary>
        /// 将<see cref="DbDataReader"/>映射为<typeparamref name="TDestination"/>列表
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static List<TDestination> To<TDestination>(this DbDataReader reader)
            where TDestination : class
        {
            return _mapper.To<TDestination>(reader, false);
        }
        /// <summary>
        /// 将<see cref="DbDataReader"/>首次读取映射为<typeparamref name="TDestination"/>
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static TDestination ToFirst<TDestination>(this DbDataReader reader)
            where TDestination : class
        {
            return _mapper.ToFirst<TDestination>(reader, false);
        }
        /// <summary>
        /// 将<see cref="object"/>映射为<typeparamref name="TDestination"/>对象
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TDestination To<TDestination>(this object source)
            where TDestination : class
        {
            return _mapper.To<TDestination>(source);
        }
        /// <summary>
        /// 将<see cref="object"/>集合映射为<typeparamref name="TDestination"/>列表
        /// </summary>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="sources"></param>
        /// <returns></returns>
        public static List<TDestination> To<TDestination>(this IEnumerable<object> sources)
            where TDestination : class
        {
            return _mapper.To<TDestination>(sources);
        }
        /// <summary>
        /// 将<typeparamref name="TSource"/>映射为<typeparamref name="TDestination"/>对象
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static TDestination Map<TSource, TDestination>(this TSource source)
            where TSource : class
            where TDestination : class
        {
            return _mapper.Map<TSource, TDestination>(source);
        }
        /// <summary>
        /// 将<typeparamref name="TSource"/>集合映射为<typeparamref name="TDestination"/>列表
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TDestination"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<TDestination> Map<TSource, TDestination>(this IEnumerable<TSource> source)
            where TSource : class
            where TDestination : class
        {
            return _mapper.Map<TSource, TDestination>(source);
        }
    }
}
